﻿using System.Collections;
using System.Data;

namespace Shipeng.Util
{
    public static partial class Extention
    {
        /// <summary>
        /// 复制序列中的数据
        /// </summary>
        /// <typeparam name="T"> 泛型 </typeparam>
        /// <param name="iEnumberable"> 原数据 </param>
        /// <param name="startIndex"> 原数据开始复制的起始位置 </param>
        /// <param name="length"> 需要复制的数据长度 </param>
        /// <returns> </returns>
        public static IEnumerable<T> Copy<T>(this IEnumerable<T> iEnumberable, int startIndex, int length)
        {
            T[] sourceArray = iEnumberable.ToArray();
            T[] newArray = new T[length];
            Array.Copy(sourceArray, startIndex, newArray, 0, length);

            return newArray;
        }

        /// <summary>
        /// 给IEnumerable拓展ForEach方法
        /// </summary>
        /// <typeparam name="T"> 模型类 </typeparam>
        /// <param name="iEnumberable"> 数据源 </param>
        /// <param name="func"> 方法 </param>
        public static void ForEach<T>(this IEnumerable<T> iEnumberable, Action<T> func)
        {
            foreach (T item in iEnumberable)
            {
                func(item);
            }
        }

        /// <summary>
        /// 给IEnumerable拓展ForEach方法
        /// </summary>
        /// <typeparam name="T"> 模型类 </typeparam>
        /// <param name="iEnumberable"> 数据源 </param>
        /// <param name="func"> 方法 </param>
        public static void ForEach<T>(this IEnumerable<T> iEnumberable, Action<T, int> func)
        {
            T[] array = iEnumberable.ToArray();
            for (int i = 0; i < array.Length; i++)
            {
                func(array[i], i);
            }
        }

        /// <summary>
        /// IEnumerable转换为List'T'
        /// </summary>
        /// <typeparam name="T"> 参数 </typeparam>
        /// <param name="source"> 数据源 </param>
        /// <returns> </returns>
        public static List<T> CastToList<T>(this IEnumerable source)
        {
            return new List<T>(source.Cast<T>());
        }

        /// <summary>
        /// 将IEnumerable'T'转为对应的DataTable
        /// </summary>
        /// <typeparam name="T"> 数据模型 </typeparam>
        /// <param name="iEnumberable"> 数据源 </param>
        /// <returns> DataTable </returns>
        public static DataTable ToDataTable<T>(this IEnumerable<T> iEnumberable)
        {
            return iEnumberable.ToJson().ToDataTable();
        }
    }
}
